<template>
    <div>
        <div>
            <el-col :span="3">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加餐桌</el-button>
            </el-col>
            <el-input style="width: 240px;" placeholder="请输入餐桌编号" v-model="params.deskNum"></el-input>
            <el-select style="margin-left: 20px" v-model="params.position" clearable placeholder="请选择餐桌位置">
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
            <el-button style="margin-left: 10px;" type="primary" icon="el-icon-search " @click="loads">搜索</el-button>
            <el-button style="margin-left: 10px;" type="warning" icon="el-icon-scissors " @click="reset">重置</el-button>
        </div>
        <br>
        <div>
            <el-col :span="6">
                <div class="grid-content bg-purple"></div>
                <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                               title="确认删除所有选中的餐桌吗？" @confirm="deleteSelected">
                    <el-button type="danger" slot="reference" v-bind:disabled="disable">删除所选餐桌</el-button>
                </el-popconfirm>
            </el-col>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%" tooltip-effect="dark"
                  @selection-change="handleSelectionChange">
            <el-table-column
                    prop="deskId"
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="deskNum" label="餐桌编号" width="180">
            </el-table-column>
            <el-table-column prop="capacity" label="容纳人数" width="180">
            </el-table-column>
            <el-table-column prop="position" label="餐桌位置" width="180">
            </el-table-column>
            <el-table-column label="操作" prop="deskId">
                <template v-slot="scope">
                    <el-button type="primary" @click="handleEdit(scope.row.deskId)">编辑
                    </el-button>
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                                   title="确认删除该餐桌吗？" @confirm="del(scope.row.deskId)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
            <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>
        <!-- 新增编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="deskform" status-icon ref="deskform"
                     style="width: 80%;"
                     label-width="200px">
                <el-form-item label="编号" prop="deskNum">
                    <el-input v-model="deskform.deskNum" placeholder="请输入餐桌编号"></el-input>
                </el-form-item>
                <el-form-item label="容纳人数" prop="capacity">
                    <el-input v-model="deskform.capacity" placeholder="请输入容纳人数"></el-input>
                </el-form-item>
                <el-form-item label="位置" prop="position">
                    <el-select v-model="deskform.position" clearable placeholder="请选择餐桌位置">
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>

                </el-form-item>

            </el-form>
            <div style="text-align-last: center;width: 60%;">
                <el-button @click="closedialog('deskform')" type="danger">取 消</el-button>
                <el-button @click="resetForm('deskform')" type="warning">重置</el-button>
                <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                           @click="dialogVisibleConfirm('deskform')" size="medium">{{save}}
                </el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'desk',
        data() {
            return {
                //将所有选中的id存全局
                multipleSelection: [],
                //后端返回的数据
                tableData: [],
                //后端返回的页数
                total: 0,
                //id存全局，判断以新增还是修改出现dialog
                allid: "",
                //dialog的标题
                title: "",
                //dialog的按钮
                save: '',
                //判断删除所选是否可用
                disable: true,
                //判断dialog是否显示
                dialogVisible: false,
                //分页模糊查询的请求体
                params: {
                    pageNum: 1,
                    pageSize: 8,
                },
                //dialog的表单
                deskform: {
                   position: '',
                    deskNum:'',
                    capacity:''
                },

                options: [
                    {
                        value: '0',
                        label: '大堂'
                    }, {
                    value: '1',
                    label: '靠窗'
                }, {
                    value: '2',
                    label: '包间'
                }],
            }
        },
        created() {
            this.load()
        },
        methods: {
            //取消所有多选
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            //判断删除所选按钮是否可用以及将已选id存全局
            handleSelectionChange(val) {
                if (val.length == 0) {
                    this.multipleSelection.length = val.length
                    this.disable = true
                } else {
                    this.disable = false
                    for (let i = 0; i < val.length; i++) {
                        this.multipleSelection[i] = val[i].deskId
                    }
                }
            },
            //新增方法
            add(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        request.post('/desk/add', this.deskform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            //修改方法
            update(formName) {
                request.put('/desk/update', this.deskform).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('修改成功')
                        this.resetForm(formName) //清空表格数据
                        this.dialogVisible = false; //关闭弹框
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            //删除方法
            del(chefAdminId) {
                request.delete('/desk/delete' + chefAdminId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        this.load();
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //重置密码方法
            resetPassword(chefAdminId) {
                request.put('/chefadmin/resetpassword' + chefAdminId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('重置成功，初始密码为123')
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            //批量删除方法
            deleteSelected() {
                console.log(this.multipleSelection)
                request.post('/desk/deletes', this.multipleSelection).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('批量删除成功')
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            //关闭dialog
            handleClose(done) {
                this.dialogVisible = false;
            },
            //添加按钮，从此处调取添加方法
            onhandAdd() {
                this.title = '添加餐桌';
                this.save = "添加"
                this.resetForm('deskform');
                this.dialogVisible = true;
            },
            //编辑按钮，从此处调取修改方法
            handleEdit(chefAdminId) {
                this.save = "保存"
                this.title = '修改餐桌信息'
                this.dialogVisible = true; //显示弹框
                request.get("/desk/" + chefAdminId).then(res => {
                    this.deskform = res.data
                        if (this.deskform.position == 0) {
                            this.deskform.position = "大堂"
                        } else if (this.tableData.position == 1) {
                            this.deskform.position = "靠窗"
                        } else {
                            this.deskform.position = "包间"
                        }

                })//row当前行数据，把当前行的数据赋值给 表单
                this.allid = chefAdminId; //把id存全局
            },
            //清空表单
            resetForm(formName) {
                this.$nextTick(() => {
                    this.$refs[formName].resetFields();
                })
            },
            //页面加载
            loads() {
                this.params.pageNum = 1
                this.load()
            },
            //初始化页面
            load() {
                request.get('/desk/page', {
                    params: this.params
                }).then(res => {
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;
                        for (let i = 0; i < this.tableData.length; i++) {
                            if (this.tableData[i].position == 0) {
                                this.tableData[i].position = "大堂"
                            } else if (this.tableData[i].position == 1) {
                                this.tableData[i].position = "靠窗"
                            } else {
                                this.tableData[i].position = "包间"
                            }
                        }
                    }
                })

            },
            //重置方法
            reset() {
                this.params = {
                    user_name: '',
                    user_num: '',
                    pageNum: 1,
                    pageSize: 10
                }
                this.load()
            },
            //核心判断哪一个按钮调用表单组件
            dialogVisibleConfirm(formName) {
                // 新增接口
                if (!this.allid) {
                    this.add(formName)

                } else {
                    // 修改接口
                    this.update(formName);
                }
            },
            //关闭dialog
            closedialog(formName) {
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },
            //切换页码
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            }
        }
    }
</script>
